<div class="form_content">
    <div class="user_permissions_section popup_form">
        <textarea id="<%= scope[:id_prefix] %>_users_and_roles_template" class="template">
            <%= scope[:form].fields_for scope[:field_name], :index => "" do |permissions_form| %>
                <td class="name_cell">
                    <%= permissions_form.text_field :name, :class => "form_input permissions_#{scope[:id_prefix]}_name", :id => nil -%>
                </td>
              <% unless @is_config_repo_pipeline %>
                <td class="name_cell">
                    <span class="icon_remove delete_parent"></span>
                </td>
              <% end %>
            <% end %>
        </textarea>
        <table class="users_and_roles">
            <tbody class="<%= scope[:id_prefix] %>_users_and_roles">
            <%- scope[:collection].each do |permission| -%>
                <%= scope[:form].fields_for scope[:field_name], permission, :index => "" do |permissions_form| %>
                    <tr>
                        <td class="name_cell">
                            <%= permissions_form.text_field :name, :class => "form_input permissions_#{scope[:id_prefix]}_name", :id => nil -%>
                            <%= error_message_on(permission, "name", :css_class => "name_value_error") %>
                        </td>
                      <% unless @is_config_repo_pipeline %>
                        <td class="name_cell align_top">
                            <span class="icon_remove delete_parent"></span>
                        </td>
                      <% end %>
                    </tr>
                <% end %>
            <%- end -%>
            </tbody>
        </table>
        <!-- <a class="add_item skip_dirty_stop" id="<%= scope[:id_prefix] %>_add_users_and_roles">Add</a> -->

		<%= action_icon({:type => "add", :text => 'Add' , :href => "javascript:void(0);", :id => scope[:id_prefix]+"_add_users_and_roles", :class => "skip_dirty_stop" }) unless @is_config_repo_pipeline %>
    </div>
</div>
<script type="text/javascript">
    Util.on_load(function() {
        function hookupPermissionsAutoComplete() {
            jQuery(<%=="\".permissions_#{scope[:id_prefix]}_name\""%>).autocomplete(<%== scope[:autocomplete_suggestions] %>, {
                minChars: 0,
                width: 400,
                matchContains: "word",
                max: 50,
                autoFill: false});
        };
        
        function setupVarForm(finish_button) {
            var rowCreator = new EnvironmentVariables.RowCreator(jQuery('#<%= scope[:id_prefix] %>_users_and_roles_template'), 'tr', '.delete_parent');
            var variables = new EnvironmentVariables(
                    jQuery('tbody.<%= scope[:id_prefix] %>_users_and_roles'),
                    rowCreator,
                    null,
                    function(inputs) {
                        inputs.dirty_form();
                        hookupPermissionsAutoComplete();
                    },
                    function(row) {
                        row.parents("form.dirtyform").data("dirty", true);
                    });

            variables.registerAddButton(jQuery("#<%= scope[:id_prefix] %>_add_users_and_roles"));
            variables.addDefaultRow();
            variables.registerFinishButton(finish_button);
        }

        var finish_button = jQuery(".finish");
        setupVarForm(finish_button);
        hookupPermissionsAutoComplete();
    });
</script>
